{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 噪声中延迟信号的互相关"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "此示例演示如何使用互相关序列来检测噪声损坏序列中的时间延迟。输出序列是具有加法白高斯噪声的输入序列的延迟版本。创建两个序列。一个序列是另一个序列的延迟版本。延迟为 3 个样本。\n",
    "延迟信号的白噪声。使用样本互相关序列来检测滞后。\n",
    "\n",
    "创建并绘制信号。将随机数生成器设置为默认设置，以获得可重现的结果。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(20, 1)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xx = np.zeros([20,1])\n",
    "x = np.arange(0.05,1,0.1)\n",
    "x = np.r_[x,x[::-1]]\n",
    "xx[:,0] = x\n",
    "xx.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "y = np.zeros([3,1])\n",
    "y.shape\n",
    "y = np.r_[y,xx]\n",
    "y = y+0.3*np.random.randn(len(x)+3,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<StemContainer object of 3 artists>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD4CAYAAAD8Zh1EAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAAAXjElEQVR4n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i/SS1hD4GPF73eCJbNuoC+L6knZLWFx1MybwlIg5C7c0MvLngeMpig6TdWUkmyfJCvyQtBpYDd1GRfpJaQleTZb7uEs6JiNOplaIukfSuogOyUrseeCtwGnAQ+Fyh0RRA0uuAbwIfi4gXio5nUFJL6BPAwrrHJwIHCoqlNCLiQPbzSeBb1EpTVnNI0gKA7OeTBcdTuIg4FBFHI+Jl4EuMWH+RNJtaMv9aRNyWLa5EP0ktod8NLJW0RNKxwPnAtoJjKpSk10p6/dR94D3AA+33GinbgAuz+xcC3y4wllKYSlyZ9zNC/UWSgC8DeyPi83WrKtFPkhspml1i9e/ALODGiPiXYiMqlqSTqR2VQ21++/8a1TaRtAVYSW0q1EPAp4GtwNeBRcBjwF9GxMicJGzRJiuplVsCeAT48FT9uOokvRP4CXA/8HK2+HJqdfTk+0lyCd3MzJpLreRiZmYtOKGbmVWEE7qZWUU4oZuZVYQTuplZRTihm5lVhBO6mVlF/D/VBNR39/Q2mAAAAABJRU5ErkJggg==",
      "text/plain": [
       "<Figure size 432x288 with 2 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.subplot(2,1,1)\n",
    "plt.stem(x)\n",
    "plt.subplot(2,1,2)\n",
    "plt.stem(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def xcorr(x,y,timelaggy):\n",
    "    x = x.flatten()\n",
    "    y = y.flatten()\n",
    "    out = np.correlate(x,y,'full')\n",
    "    midIndex = int(len(out)/2)\n",
    "    mid = out[midIndex]\n",
    "    autocor = out/mid\n",
    "    if timelaggy>len(out)/2:\n",
    "        autocor = autocor\n",
    "        lags = np.linspace(-len(out)/2,len(out)/2,2*len(out)+1  )\n",
    "    else :\n",
    "        autocor = autocor[midIndex-timelaggy:midIndex+timelaggy]\n",
    "        lags = np.arange(-timelaggy,timelaggy,1)\n",
    "    return autocor,lags"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "获取样本互相关序列，并使用最大绝对值来估计滞后。绘制样本互相关序列。正如预期的那样，最大互相关序列值出现在滞后 3 处。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xc,lags = xcorr(y,x,21)\n",
    "I = np.argmax(np.abs(xc))\n",
    "lags[I]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x20dfcb01f98>]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD4CAYAAAD8Zh1EAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAAAUQklEQVR4nO3df6zddX3H8efbIqQb06q9OnpppSaV2Q1c9Qo6tozpXAuatRJNgMU5NlOb0MUlC6PMTJeYhS5ki3GitXFElxg6M1ntpNopzOE0zF5WBpSurisB7i2RoqtObcTCe3/c0/b03nPP+d7e77nne77f5yNpuN8f59xPv9z76ue8v5/P5xuZiSRp+L1g0A2QJJXDQJekmjDQJakmDHRJqgkDXZJq4pxBfeOlS5fmRRddNKhvL0lD6YEHHngmM0c6HRtYoF900UWMj48P6ttL0lCKiMdnO2bJRZJqwkCXpJow0CWpJgx0SaoJA12SamJgo1ykYbdz3yS37TnIkWPHWbZkMTetvZgNa0YH3Sw1mIEunYWd+ya55a6HOf7T5wCYPHacW+56GMBQ18BYcpHOwm17DnL8p8/xwa9u54Nf3Q7A8Z8+x217Dg64ZWoye+jSWThy7DgAq58+3HG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4U2O7IAAAAASUVORK5CYII=",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.stem(lags,xc)\n",
    "plt.plot([lags[I],lags[I]],[xc[I],0],'r')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "I = np.argmax(np.abs(xc))\n",
    "lags[I]"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "527a93331b4b1a8345148922acc34427fb7591433d63b66d32040b6fbbc6d593"
  },
  "kernelspec": {
   "display_name": "Python 3.6.13 ('pytorch')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
